using System;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Serialization;

namespace Bullet
{
    public class EnemyBullet : MonoBehaviour
    {
        private readonly float moveSpeed = 10f;
        public Vector3 dir = Vector3.zero;
        [FormerlySerializedAs("AttackRange")] public float attackRange;

        private void Start()
        {
            Vector3 originalPos = transform.position;//保存初始位置
            dir = transform.forward;
            transform.position = originalPos;
        }

        private void OnEnable()
        {
            Vector3 originalPos = transform.position;//保存初始位置
            dir = transform.forward;
            transform.position = originalPos;
        }

        void Update()
        {
            if(Player.Player.instance != null)
                MoveToPlayer();
        }
        //向敌人的位置移动
        public void MoveToPlayer()
        {
            //向dir方向移动
            transform.Translate(dir * moveSpeed * Time.deltaTime, Space.World);
        }

        private void OnTriggerEnter(Collider other)
        {
            //检测是否碰到玩家
            if (other.gameObject.CompareTag("Player"))
            {
                Player.Player.instance.HitPlayer(attackRange);
                gameObject.SetActive(false);
            }
            //检测是否碰到墙
            if (other.gameObject.CompareTag("Wall"))
            {
                gameObject.SetActive(false);
            }
        }
    }
}
